package cn.com.yx.ms.service.impl;

import cn.com.yx.ms.application.web.ApiQueryBase;
import cn.com.yx.ms.domain.dbmodel.SysDict;
import cn.com.yx.ms.domain.sys.DictChildrenModel;
import cn.com.yx.ms.domain.sys.DictTreeModel;
import cn.com.yx.ms.persistence.SysDictMapper;
import cn.com.yx.ms.service.IDictService;
import cn.yx.framework.configuration.AppConstant;
import cn.yx.framework.view.ApiResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lenovo on 2017/5/2.
 */
@Service
@Transactional
public class DictServiceImpl implements IDictService {
    @Resource
    private SysDictMapper sysDictMapper;

    @Override
    public ApiResult saveDictionary(SysDict sysDict) {
        //先判断类型是否存在
        SysDict sysDict1 = sysDictMapper.findByDictvalue(sysDict.getDictvalue());
        if (sysDict1 != null) {
            //已经存在这个名称,不能添加
            return new ApiResult<>(AppConstant.MSG_FAILURE, false, "已存在,无法添加");
        } else {
            //不存在,进行新增
            try {
                sysDictMapper.insertDictionary(sysDict);
            } catch (Exception e) {
                return new ApiResult<>(AppConstant.MSG_FAILURE, false, "字典类型字符超出长度,添加失败");
            }
            return new ApiResult<>(AppConstant.MSG_SUCCESS, true, "添加成功");
        }
    }

    @Override
    public int deleteDic(String dictvalue) {
        int result = sysDictMapper.deleteDictionary(dictvalue);
        int result1 = sysDictMapper.deleteDictionarydata(dictvalue);
        SysDict sysDict = sysDictMapper.findByDictvalue(dictvalue);
        if (sysDict != null) {
            return -1;
        }
        System.out.println("result+result1:" + result + result1);
        return result + result1;
    }

    @Override
    public SysDict findByDictvalue(String dictvalue) {
        return sysDictMapper.findByDictvalue(dictvalue);
    }

    @Override
    public ApiResult updateDictionary(SysDict sysDict) {
        return new ApiResult<>(AppConstant.MSG_SUCCESS, true, "", sysDictMapper.updateDictionary(sysDict));
    }

    @Override
    public ApiResult saveDictionarydata(SysDict sysDict) {
        //判断是否有这个类型的dictvalue
        SysDict sysDict1 = sysDictMapper.findByDictName(sysDict.getDictname());
        if (null != sysDict1) {
            //进入新增
            sysDict.setDictvalue(sysDict1.getDictvalue());
            return new ApiResult<>(AppConstant.MSG_SUCCESS, true, "", sysDictMapper.insertDictionarydata(sysDict));
        } else {
            return new ApiResult<>(AppConstant.MSG_FAILURE, false, "找不到该类型的字典名!");
        }

    }

    @Override
    public List<DictTreeModel> findAllDictionary(ApiQueryBase apiQueryBase) {
        //System.out.println(apiQueryBase.getConditions());
        List<SysDict> sysDicts = sysDictMapper.findAllDictionary(apiQueryBase);
        List<DictChildrenModel> dictChildrenModels = new ArrayList<DictChildrenModel>();
        for (SysDict sysDict : sysDicts) {
            DictChildrenModel dictChildrenModel = new DictChildrenModel();
            dictChildrenModel.setName(sysDict.getDictname());
            dictChildrenModel.setId(sysDict.getDictvalue());
            dictChildrenModels.add(dictChildrenModel);
        }
        List<DictTreeModel> dictTreeModels = new ArrayList<DictTreeModel>();
        //初始化根节点
        DictTreeModel root = new DictTreeModel();
        root.setName("字典类型");
        root.setChildren(new ArrayList<DictChildrenModel>());
        root.setChildren(dictChildrenModels);
        dictTreeModels.add(root);

        return dictTreeModels;
    }

    @Override
    public List<SysDict> findAllDinctionarydata(ApiQueryBase queryBase) {
        List<SysDict> sysDictList = null;
        if (null != queryBase.getConditions().get("dictvalue") && queryBase.getConditions().get("dictvalue").equals("D1004g")) {
            queryBase.getConditions().put("dictvalue", "D1004");
            sysDictList = sysDictMapper.findAllDinctionarydata(queryBase);
            SysDict sysDict = new SysDict();
            sysDict.setDictdatavalue("全部");
            sysDict.setDictdataname("-1");
            List<SysDict> sysDicts = new ArrayList<SysDict>();
            sysDicts.add(sysDict);
            sysDicts.addAll(sysDictList);
            return sysDicts;
        } else {
            sysDictList = sysDictMapper.findAllDinctionarydata(queryBase);
        }
        return sysDictList;
    }

    public List<SysDict> findAllDictDatas(String dictid) {
        return sysDictMapper.findAllDictDatas(dictid);
    }

    @Override
    public int delDictionarydata(String id) {
        return sysDictMapper.deleteDinctionaryData(id);
    }

    @Override
    public ApiResult updateDictionarydata(SysDict sysDict) {
        SysDict sysDict1 = sysDictMapper.findByDictName(sysDict.getDictname());
        if (null != sysDict1) {

            sysDict.setDictvalue(sysDict1.getDictvalue());
            return new ApiResult<>(AppConstant.MSG_SUCCESS, true, "", sysDictMapper.updateDictionaryData(sysDict));
        }
        return new ApiResult<>(AppConstant.MSG_FAILURE, false, "找不到该字典名!");
    }

    @Override
    public SysDict findOneSysDictById(String id) {
        return sysDictMapper.findDictDataById(id);
    }

}
